package cn.pzaily.fealetter.service.impl;

import cn.pzaily.crab.tools.core.utils.ObjectUtils;
import cn.pzaily.fealetter.entity.TemplateEntity;
import cn.pzaily.fealetter.enumeration.RedisPropertiesKey;
import cn.pzaily.fealetter.mapper.TemplateMapper;
import cn.pzaily.fealetter.service.TemplateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @PackageName: cn.pzaily.fealetter.service.impl
 * @Description:
 * @Author: Pang
 * @Date: 2021-11-21 20:17
 */
@Service
public class TemplateServiceImpl extends
        ServiceImpl<TemplateMapper, TemplateEntity> implements TemplateService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public TemplateEntity findTemplateByCode(String code) {
        ValueOperations<String, TemplateEntity> ops = redisTemplate.opsForValue();
        TemplateEntity templateEntity = ops.get(RedisPropertiesKey.TEMPLATE_CODE + code);
        if (ObjectUtils.isNull(templateEntity)) {
            templateEntity = baseMapper.selectOne(new LambdaQueryWrapper<TemplateEntity>().eq(TemplateEntity::getCode, code));
            if (ObjectUtils.isNotNull(templateEntity)) {
                ops.set(RedisPropertiesKey.TEMPLATE_CODE + templateEntity.getCode(), templateEntity,
                        RedisPropertiesKey.TIME, RedisPropertiesKey.TIME_UNIT);
            }
        }
        return templateEntity;
    }
}
